package com.situ.mvc.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.situ.mvc.entity.Student;

//  请求映射，怎么样找到这些请求的路径 /student/insert.action

// 项目启动的时候SpringMVC会扫描com.situ.mvc下面所有类的注解
// 当扫描到StudentController时候会记录下来：/student/insert  /student/deleteById
@Controller
// @RequestMapping(value = "/student")
@RequestMapping("/student")
public class StudentController {

	@RequestMapping("/insert1")
	public void insert1(String name, Integer age, String gender) {
		System.out.println("StudentController.insert1()");
		System.out.println("name: " + name);
		System.out.println("age: " + age);
		System.out.println("gender: " + gender);
		Student student = new Student(name, age, gender);
	}

	@RequestMapping("/insert2")
	public ModelAndView insert2(Student student) {
		System.out.println("StudentController.insert2()");
		System.out.println(student);

		// req.setAttritbu("student", student);
		// req.getRequestDispatcher("/student_info.jsp").forword(req, resp);

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("student", student);
		modelAndView.setViewName("/student_info.jsp");
		return modelAndView;
	}

	@RequestMapping("/insert3")
	public String insert3(Student student, Model model) {
		System.out.println("StudentController.insert3()");
		System.out.println(student);

		// req.setAttritbu("student", student);
		model.addAttribute("student", student);
		// req.getRequestDispatcher("/student_info.jsp").forword(req, resp);
		return "/student_info.jsp";
	}

	// 默认情况下既可以支持get请求也可以支持post请求，
	// 如果希望这个方法只接受post请求：method = RequestMethod.POST
	// @RequestMapping("/insert")
	@RequestMapping(value = "/insert4", method = RequestMethod.POST)
	// @RequestMapping(value = "/insert", method = {RequestMethod.POST,
	// RequestMethod.GET})
	public String insert4(Student student, Model model) {
		System.out.println("StudentController.insert4()");
		System.out.println(student);

		// req.setAttritbu("student", student);
		model.addAttribute("student", student);
		// req.getRequestDispatcher("/student_info.jsp").forword(req, resp);
		return "/student_info.jsp";
	}

	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public void insert(HttpServletRequest request, HttpServletResponse response, HttpSession session)
			throws ServletException, IOException {
		System.out.println("StudentController.insert()");
		String name = request.getParameter("name");
		String age = request.getParameter("age");
		String gender = request.getParameter("gender");
		Student student = new Student(name, Integer.parseInt(age), gender);

		request.setAttribute("student", student);
		request.getRequestDispatcher("/student_info.jsp").forward(request, response);

	}
	
	
	@RequestMapping("/selectAll")
	public String selectAll() {
		System.out.println("StudentController.selectAll()");
		
		return "/student_info.jsp";
	}

    // /Java2012SpringMVC/student/deleteById.action?id=3 
	@RequestMapping("/deleteById")
	public String deleteById(Integer id) {
		System.out.println("StudentController.deleteById()");
		System.out.println("id: " + id);
		
		// response.sendRedirect("/Java2012SpringMVC/student/selectAll.action");
		return "redirect:/student/selectAll.action";
	}
	
	// http://localhost:8080/Java2012SpringMVC/student/deleteAll.action?ids=1&ids=2&ids=4
	@RequestMapping("/deleteAll1")
	public String deleteAll1(HttpServletRequest request) {
		System.out.println("StudentController.deleteAll1()");
		String[] ids = request.getParameterValues("ids");
		System.out.println(Arrays.toString(ids));
		 
		return "/student_info.jsp";
	}
	
	@RequestMapping("/deleteAll")
	public String deleteAll(Integer[] ids) {
		System.out.println("StudentController.deleteAll()");
		System.out.println(Arrays.toString(ids));
		
		return "/student_info.jsp";
	}
	
	@RequestMapping("/selectById")
	@ResponseBody
	public Student selectById(Integer id) {
		System.out.println("StudentController.selectById()");
		System.out.println("id: " + id);
		Student student = new Student(1, "zhansgan", 23, "男");
		
		return student;
	}
	
	@RequestMapping("/selectByPage")
	@ResponseBody
	public List<Student> selectByPage(
			@RequestParam(/* defaultValue = "1", */ value = "pageNo", required = true)Integer pageNo, Integer pageSize) {
		System.out.println("StudentController.selectByPage()");
		System.out.println("pageNo: " + pageNo);
		
		Student student1 = new Student(1, "zhansgan1", 23, "男");
		Student student2 = new Student(1, "zhansgan2", 23, "男");
		Student student3 = new Student(1, "zhansgan3", 23, "男");
		Student student4 = new Student(1, "zhansgan4", 23, "男");
		List<Student> list = new ArrayList<Student>();
		list.add(student1);
		list.add(student2);
		list.add(student3);
		list.add(student4);
		
		return list;
	}
	
	// /Java2012SpringMVC/student/deleteById.action?id=3 
	// /Java2012SpringMVC/student/deleteById/2.action
	@RequestMapping("/deleteById/{id}")
	public String deleteById2(@PathVariable(value = "id")Integer id) {
		System.out.println("StudentController.deleteById2()");
		System.out.println("id: " + id);
		
		// response.sendRedirect("/Java2012SpringMVC/student/selectAll.action");
		return "redirect:/student/selectAll.action";
	}
	
	@RequestMapping("/select")
	public String select() {
		System.out.println("StudentController. selectById()");
		
		// return "/jsp/test.jsp";
		return "test";
	}
}
